// a-z排序工具类
export function pySort(arr, empty) {
  if (!String.prototype.localeCompare) return null
  var letters = 'ABCDEFGHJKLMNOPQRSTWXYZ#'.split('')
  var zh = '阿八嚓哒妸发旮哈讥咔垃妈拏噢妑七呥扨它穵夕丫帀'.split('')
  var arrList = []
  for (var m = 0; m < arr.length; m++) {
    arrList.push(arr[m])
  }
  var result = []
  var curr
  const specialCharCategory = {
    letter: '#',
    data: []
  }
  for (var i = 0; i < letters.length; i++) {
    curr = {
      letter: letters[i],
      data: []
    }
    for (var j = 0; j < arrList.length; j++) {
      var initial = arrList[j].name.charAt(0) // 截取第一个字符
      let found = false
      if (
        arrList[j].name.charAt(0) == letters[i] ||
        arrList[j].name.charAt(0) == letters[i].toLowerCase()
      ) {
        // 首字符是英文的
        found = true
        curr.data.push(arrList[j])
      } else if (zh[i] !== '*' && isChinese(initial)) {
        // 判断是否是汉字,是否是中文
        if (
          initial.localeCompare(zh[i]) >= 0 &&
          (!zh[i + 1] || initial.localeCompare(zh[i + 1]) < 0)
        ) {
          // 判断中文字符在哪一个类别
          found = true
          curr.data.push(arrList[j])
        }
      } else {
        if (!isChar(initial) && !isChinese(initial)) {
          specialCharCategory.data.push(arrList[j])
        }
      }
    }
    if (empty || curr.data.length) {
      result.push(curr) // curr.data.sort(function(a,b){ // return b.localeCompare(a); //排序,英文排序,汉字排在英文后面 //});
    }
  }
  if (specialCharCategory.data.length) {
    specialCharCategory.data = [...new Set(specialCharCategory.data)]
    result[result.length] = specialCharCategory
  }

  return result
}

function isChinese(temp) {
  var re = /[^\u4E00-\u9FA5]/
  if (re.test(temp)) {
    return false
  }
  return true
}

function isChar(char) {
  var reg = /[A-Za-z]/
  if (!reg.test(char)) {
    return false
  }
  return true
}

// // 数据埋点工具类
// export function abcSetAnchorC(key, value) {
//   my.call(
//     "abcSetAnchor", {
//     key,
//     value
//   },
//     response => {
//       console.log(response)
//     }
//   )
// }
export function pySortByList(list) {
  const result = []
  for (let i = 0; i < list.length; i++) {
    if (findIndex(result, list[i].acronymPinyin) > -1) {
      // 结果列表中包含这个字符
      const index = findIndex(result, list[i].acronymPinyin)
      result[index].data.push(list[i])
    } else {
      // 结果列表中不包含这个字符，将字符加进去
      if (list[i].acronymPinyin == null) {
        // 直接过滤
      } else {
        const temp = {
          letter: list[i].acronymPinyin,
          data: [list[i]]
        }
        result.push(temp)
      }
    }
  }

  return compar(result)
}

function findIndex(list, char) {
  let result = -1
  if (list.length > 0) {
    list.some((item, index) => {
      if (item.letter != undefined && item.letter == char) {
        result = index
      }
    })
  }
  return result
}

function compar(array) {
  return array.sort(function(a, b) {
    const x = a.letter
    const y = b.letter
    return x.localeCompare(y)
  })
}

// 获取机型
export function getDeviceType() {
  const userAgent = navigator.userAgent || navigator.vendor || window.opera

  // 鸿蒙系统 User Agent 示例：Mozilla/5.0 (Linux; Android 10; HMA-AL00 Build/HUAWEIHMA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Mobile Safari/537.36鸿蒙
  if (userAgent.includes('HarmonyOS')) {
    return 'HarmonyOS'
  }

  // iOS User Agent 示例：Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.3 Mobile/15E148 Safari/604.1
  if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
    return 'iOS'
  }

  // 安卓系统 User Agent 示例：Mozilla/5.0 (Linux; Android 11; Pixel 4 XL Build/RP1A.201005.004) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Mobile Safari/537.36
  if (/android/i.test(userAgent)) {
    return 'Android'
  }

  // 默认返回其他类型
  return 'Other'
}

// 获取机型
export function getABCDeviceType() {
  let userAgent

  if (window.AlipayJSBridge) {
    window.AlipayJSBridge.call('abcGetInfo', {
      name: 'platform'
    }, function(result) {
      userAgent = result.platform
    })
  } else {
    document.addEventListener('AlipayJSBridgeReady', () => {
      console.log('回调AlipayJSBridge')
      window.AlipayJSBridge.call('abcGetInfo', {
        name: 'platform'
      }, function(result) {
        userAgent = result.platform
      })
    }, false)
  }
  return userAgent
}
